//
//  RatingVC.m
//  ImGuider
//
//  Created by llt on 2017/6/22.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "RatingVC.h"
#import "StarRateView.h"
#import <IQTextView.h>
#import "LineQuestionHeadView.h"
#import <IQKeyboardManager.h>
@interface RatingVC () <StarRateViewDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet StarRateView *rateView;

@property (nonatomic) CGFloat score;
//@property (nonatomic, strong) NSArray *messageArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *lineView;

@property (weak, nonatomic) IBOutlet IQTextView *commentTextView;
@property (weak, nonatomic) IBOutlet UILabel *CharLimitLabel;

@property (weak, nonatomic) IBOutlet LineQuestionHeadView *LineQuestionHeadView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toTopSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelToTopSapce;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textH;

@property (nonatomic, strong) UIButton * commitBtn;
@property (nonatomic, copy) void (^ commitBlock) (CGFloat score, NSString *comment);
@end

@implementation RatingVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    self.commentTextView.layer.borderWidth = 1.0;
    self.commentTextView.layer.borderColor = kBGColor.CGColor;
    
    if (ISiPhone5 || ISiPhone4) {
        
        self.textH.constant = 120;
    }
    [self setup];
}

- (void)setup {
    
    self.titleLabelToTopSapce.constant = kNavigationBarHeight + 20;
    self.toTopSpace.constant = kNavigationBarHeight;
    self.titleLabel.textColor = kTitleColor;
    self.CharLimitLabel.textColor = kContentColor;
    self.commentTextView.textColor = kContentColor;
    self.rateView.delegate = self;
    self.rateView.canRating = YES;
    [self.rateView setScore:4];
    
    self.titleLabel.text = MYLocalizedString(@"PUBLIC_COMMENT_CONTENT", nil);
    
    // [self.commentedBtn setTitle:MYLocalizedString(@"PUBLIC_OK", nil) forState:UIControlStateNormal];
    
    //self.commentedBtn.hidden = YES;
    
    if (self.isLineQuestion) {
        self.title = MYLocalizedString(@"SITEPLAY_SUBMIT_QUESTION", nil);;
        
        self.LineQuestionHeadView.lineName = self.lineModel.linename;
        self.LineQuestionHeadView.headerImageName = self.lineModel.guide.headimg;
        self.LineQuestionHeadView.vip = self.lineModel.guide.vip;
        
        self.LineQuestionHeadView.guiderName = self.lineModel.guide.realname;
        self.commentTextView.placeholder =MYLocalizedString(@"SITEPLAY_QUESTION_TIPS", nil);// SITEPLAY_QUESTION_TIPS
        
        [self rightBarButton:MYLocalizedString(@"PUBLIC_COMMENT_COMMIT", nil) color:[UIColor whiteColor] selector:@selector(commitInfor)];
        [self.commentTextView becomeFirstResponder];
    } else {
        self.title = MYLocalizedString(@"SITEPLAY_WRITE_COMMENT", nil);
        
        self.LineQuestionHeadView.hidden = YES;
        self.commentTextView.placeholder =MYLocalizedString(@"SITEPLAY_COMMENT_TIPS", nil);
        
        if (_hasscore) {
            
            [_rateView setScore:_hasscore/10.0];
            _commentTextView.text = self.comment ? self.comment : @" ";
            _CharLimitLabel.hidden = YES;
            _rateView.userInteractionEnabled = NO;
            _commentTextView.userInteractionEnabled = NO;
            
        }else {
            
            [self rightBarButton:MYLocalizedString(@"PUBLIC_COMMENT_COMMIT", nil) color:[UIColor whiteColor] selector:@selector(commitInfor)];
            [self.commentTextView becomeFirstResponder];
        }
    }
}


- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}
//- (void)viewWillAppear:(BOOL)animated {
//    
//    [super viewWillAppear:animated];
//    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
//}
- (void)setHasscore:(NSInteger)hasscore {
    
    _hasscore = hasscore;
    
    [_rateView setScore:_hasscore/10.0];
    _commentTextView.text = self.comment ? self.comment : @" ";
    _CharLimitLabel.hidden = YES;
    _rateView.userInteractionEnabled = NO;
    _commentTextView.userInteractionEnabled = NO;
}


- (void)commitInfor {
    // 提交
    
    NSString * str = self.commentTextView.text.copy;
    
    NSString * result = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (result.length <= 0 && self.isLineQuestion) {
        
        [IGUtils showToast:MYLocalizedString(@"SITEPLAY_COMMENT_EMPTY", nil)];
        return;
    }
    if (self.commitBlock) {
        
        self.commitBlock(self.score, self.commentTextView.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)show:(UIViewController *)vc completeBlock:(void (^) (CGFloat score, NSString *comment))block {
    
    
    self.commitBlock = block;
    
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;
    
    [vc.navigationController pushViewController:self animated:YES];
}


-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

#pragma mark -StarRateViewDelegate

- (void)starRatingView:(StarRateView *)view score:(float)score {
    
//    NSInteger newScore = ceil(score * 5);
    
//    NSInteger index = newScore > 0 ? newScore - 1 : 0;
//    NSString *message = self.messageArray[index];
    
    
    self.score = score * 10;
}


#pragma mark - UITextViewDelegate
#define MAX_STARWORDS_LENGTH 100

- (void)textViewDidChange:(UITextView *)textView {
    
    NSString * string  =  textView.text;
    
    //获取高亮部分
    
    UITextRange * seletedRange = [textView markedTextRange];
    
    UITextPosition * position = [textView positionFromPosition:seletedRange.start offset:0];
    
    // 没有高亮选择的字， 就对其进行字数统计和限制
    
    if (!position) {
        
        if (string.length > MAX_STARWORDS_LENGTH) {
            
            NSRange rangeIndex = [string rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1) {
                
                textView.text = [string substringToIndex:MAX_STARWORDS_LENGTH];
            }else {
                
                NSRange rangeRange = [string rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                
                textView.text = [string substringWithRange:rangeRange];
            }
        }
    }
    int length =(int)(MAX_STARWORDS_LENGTH - textView.text.length);
    
    if (length >=0) {
        
        self.CharLimitLabel.text = @(MAX_STARWORDS_LENGTH - textView.text.length).stringValue;
    }
    
}

@end
